package org.example.springTest;


import org.example.springTest.event.RandomEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;

@RestController
public class MyController {

    @Autowired
    private ApplicationEventPublisher publisher;


    @PostConstruct
    public void init() {
        System.out.println("MyController init");
        RandomEvent event = new RandomEvent("test初始化");
        publisher.publishEvent(event);
    }


    //可以在任何地方发布事件 代替直接调用 相当于 内部的消息队列啥的
    @GetMapping("/test")
    public String test() {
        System.out.println("MyController test");
        RandomEvent event = new RandomEvent("test");
        publisher.publishEvent(event);
        return "success";
    }



}
